#include "main/utils.h"
using namespace std;

char firstTimeOccurInString(const string &str) {
  map<char, int> hash_map;
  for (int i = 0; i < str.size(); ++i) {
    if (hash_map.find(str[i]) != hash_map.end()) {
      ++hash_map[str[i]];
    } else {
      hash_map.insert(make_pair(str[i], 1));
    }
  }

  for (int i = 0; i < str.size(); ++i) {
    if (hash_map[str[i]] == 1) {
      return str[i];
    }
  }
  return '\0';
}

char firstTimeOccurInStream(const string &str) {
  map<char, int> hash_map;
  for (int i = 0; i < str.size(); ++i) {
    if (hash_map.find(str[i]) != hash_map.end()) {
      hash_map[str[i]] = -1;
    } else {
      hash_map.insert(make_pair(str[i], i));
    }
  }

  int first_occur_idx = INT_MAX;
  char first_occur_char = '\0';
  for (auto pair : hash_map) {
    if (pair.second != -1 && pair.second < first_occur_idx) {
      first_occur_char = pair.first;
      first_occur_idx = pair.second;
    }
  }

  return first_occur_char;
}

int main() {
  string str = "abaccdeff";
  char ret_string = firstTimeOccurInString(str);
  char ret_stream = firstTimeOccurInStream(str);
  cout << "The char first time occur in string: " << ret_string << endl;
  cout << "The char first time occur in stream: " << ret_stream << endl;

  return 0;
}
